//
//  ViewController.m
//  AVPlayerDemo
//
//  Created by UniHan_iMac on 17/4/13.
//  Copyright © 2017年 UniHan_iMac. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@interface ViewController ()<AVPlayerViewControllerDelegate>
{
    AVPlayer *_player;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self foundationPlayer];
    [self kitPlayer];
}

- (void)foundationPlayer {
    // 创建要播放的元素
    NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"];
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    
    // 创建播放器
    _player = [AVPlayer playerWithPlayerItem:item];
    // 创建视频显示的图层
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
    layer.frame = self.view.frame;
    [self.view.layer addSublayer:layer];
    // 开始播放
    [_player play];

    // 采取kvo的形式获取视频总时长
    // 通过监听status判断是否准备好
    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    
    // 快进功能:获得当前时间，然后令它自加5秒，并跳转到这个时间
    Float64 currentTime = CMTimeGetSeconds(_player.currentTime);
    [_player seekToTime:CMTimeMake(currentTime,1)];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"%@",change[@"new"]);
    // 得到视频时长的时候，获得的值是一个CMTime类型的值，它表示视频的播放进度，它有两个属性:value(进度);timescale(帧率)
    switch ([change[@"new"]integerValue]) {
        case 0:
            NSLog(@"未知状态");
            break;
        case 1:
            NSLog(@"获得视频总时长:%f",CMTimeGetSeconds(_player.currentItem.duration));
            break;
        case 2:
            NSLog(@"加载失败");
            break;
        default:
            break;
    }
}

- (void)kitPlayer
{
    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4"]];
    AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
    
    playerVC.player = player;
    playerVC.view.frame = CGRectMake(0, 50, screenWidth, 400);
    [self.view addSubview:playerVC.view];
    
    // AVLayerVideoGravityResizeAspectFill:自适应视图大小
    player.externalPlaybackVideoGravity = AVLayerVideoGravityResizeAspectFill;
    playerVC.delegate = self;
}
#pragma mark 视频将要开始播放的时候调用
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"1视频将要开始播放的时候调用");
}
#pragma mark 视频已经开始播放的时候调用
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"2视频已经开始播放的时候调用");
}
#pragma mark 视频将要停止的时候调用
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"3视频将要停止的时候调用");
}
#pragma mark 视频已经停止的时候调用
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"4视频已经停止的时候调用");
}
#pragma mark 视频播放失败的时候调用
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {
}
@end
